// pages/homepagesearch/index.js
const app = getApp()

const {
  searchindexlist,
} = require('../../utils/api')
const { fetch, newFetch } = require('../../utils/fetch')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinceList:[],
    specificarea:null,
    value:'',
    isAddress:true,
    arrcity: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }
    this.initData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  
    
    initData:function() {
      var that = this;
      var cityname = wx.getStorageSync('citydetail')
      fetch(`wx/areainfo/${this.data.specificarea}/getAreaByCityName`, 'POST',{
        cityName:cityname.name
      }).then(res => {
        if(res) {
          that.setData({
            provinceList:res
          })
        }else {
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 1500
        })
        }
      })
    },

    goPrivce:function(e){
      var that = this
      var navkey = e.currentTarget.dataset.pridata
        // wx.setStorageSync('navcity', navkey)
        var data={
          district: navkey.name,
          adcode:navkey.code,
        }
      var listval = {
        code: navkey.code,
        id: navkey.id,
        isagent: navkey.isagent,
        name: navkey.name,
        firstletter: navkey.firstletter
      }
       var arrList = wx.getStorageSync('arrcity') || []
       var selarea = wx.getStorageSync('selarea') || {}
       var isFeed = false
       arrList && arrList.map(val => {
         if(val.name == navkey.name || val.name == selarea.district) {
          console.log(val.name, selarea.district, '老师的看法就', selarea)
           isFeed = true
         }
       })
       console.log(isFeed, '老师的看法就路上看到积分路上看到房价')
       if(!isFeed) {
        console.log(arrList, '老师的会计法', selarea)

         arrList.unshift(listval)
         wx.setStorageSync('arrcity', arrList)
       }
      //  return
        app.set_sel_area(data);
        newFetch(`wx/member/${this.data.specificarea}/updateArea`, 'POST', {  memberId: wx.getStorageSync('userInfo').id,county: data.adcode,countyname:data.district }).then(res=>{
          if(res.code == 200) {
              // 判断我是否是当前社区的代理商
              newFetch(`wx/member/${data.adcode}/agentMyArea`, 'POST', {  memberId: wx.getStorageSync('userInfo').id,area: data.adcode }).then(res=>{
                if(res.code == 200) {
                   console.log(res.data.isagent, '222222222222')
                   let userInfo = wx.getStorageSync('userInfo')
                   userInfo = Object.assign({}, userInfo, {
                       area: data.adcode,
                       areaname: data.district,
                       isagent: res.data.isagent
                   })
                   wx.setStorageSync('userInfo', userInfo)
                   wx.setStorageSync('selarea', data)
                }
              })
            // let userInfo = wx.getStorageSync('userInfo')
            // userInfo = Object.assign({}, userInfo, {
            //     area: data.adcode,
            //     areaname: data.district
            // })
            // wx.setStorageSync('userInfo', userInfo)
            // wx.setStorageSync('selarea', data)
          }
        })
        wx.reLaunch({
          url: '/pages/index/index?selarea='+JSON.stringify(data)
        })
    },
    // 获取搜索输入内容
    input(e) {
        this.value = e.detail.value;
    },

    searchMt:function(){
      var that = this;
      fetch(`wx/areainfo/${this.data.specificarea}/queryAreaList`, 'POST',{
          searchName:this.value
      }).then(res => {
        if(res) {
            that.setData({
              isAddress:false,
              searchdataList:res
            })
        }else {
          wx.showToast({
            title: '暂无搜索数据',
            icon: 'none',
            duration: 1500
        })
        }
      })
    },
    goaddress(e){
      var navkey = e.currentTarget.dataset.pridata
      if(navkey.code.slice(4)== '00') {
        wx.setStorageSync('citydetail', navkey)
        wx.navigateTo({
          url: '/pages/dingprivince/index',
        })
    } else {
        var data={
            district: navkey.name,
            adcode:navkey.code,
          }
          newFetch(`wx/member/${this.data.specificarea}/updateArea`, 'POST', {  memberId: wx.getStorageSync('userInfo').id,county: data.adcode, countyname:data.district }).then(res=>{
            if(res.code == 200) {
                // 判断我是否是当前社区的代理商
                newFetch(`wx/member/${data.adcode}/agentMyArea`, 'POST', {  memberId: wx.getStorageSync('userInfo').id,area: data.adcode }).then(res=>{
                  if(res.code == 200) {
                      console.log(res.data.isagent, '333333333333333333333333')
                      let userInfo = wx.getStorageSync('userInfo')
                      userInfo = Object.assign({}, userInfo, {
                          area: data.adcode,
                          areaname: data.district,
                          isagent: res.data.isagent
                      })
                      wx.setStorageSync('userInfo', userInfo)
                      wx.setStorageSync('selarea', data)
                  }
                })
            }
          })
          app.set_sel_area(data);
          wx.reLaunch({
            url: '/pages/index/index?selarea='+JSON.stringify(data)
          })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})